package com.gitee.blogcase.simplerabbitmqcase.consumer;

import com.gitee.blogcase.simplerabbitmqcase.producer.ExchangeDirectProducer;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 直连交换机队列消费者
 * 直连交换机与扇形交换机的区别在于,队列都是绑定同一个交换机,但是在队列上会添加routingkey标识,消费者会根据对应的不同routingkey消费对应的消息。
 * @author wuwentao
 */
@Component
@Slf4j
public class ExchangeDirectConsumer {
    /**
     * 创建交换机并且绑定队列1（绑定routingkey1）
     *
     * @param content 内容
     * @param channel 通道
     * @param message 消息
     * @throws IOException      ioexception
     * @throws TimeoutException 超时异常
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = ExchangeDirectProducer.EXCHANGE_DIRECT, durable = "true", type = ExchangeTypes.DIRECT),
            value = @Queue(value = ExchangeDirectProducer.EXCHANGE_DIRECT_QUEUE_1, durable = "true"),
            key = ExchangeDirectProducer.EXCHANGE_DIRECT_ROUTING_KEY_1
    ))
    @RabbitHandler
    public void exchangeDirectRoutingKey1(String content, Channel channel, Message message) {
        log.info("队列1 KEY1接收到消息：{}",content);
    }

    /**
     * 创建交换机并且绑定队列2（绑定routingkey2）
     *
     * @param content 内容
     * @param channel 通道
     * @param message 消息
     * @throws IOException      ioexception
     * @throws TimeoutException 超时异常
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = ExchangeDirectProducer.EXCHANGE_DIRECT, durable = "true", type = ExchangeTypes.DIRECT),
            value = @Queue(value = ExchangeDirectProducer.EXCHANGE_DIRECT_QUEUE_2, durable = "true"),
            key = ExchangeDirectProducer.EXCHANGE_DIRECT_ROUTING_KEY_2
    ))
    @RabbitHandler
    public void exchangeDirectRoutingKey2(String content, Channel channel, Message message) {
        log.info("队列2 KEY2接收到消息：{}",content);
    }

}
